package org.swidgets.client;




import java.util.Date;

import org.swidgets.base.forms.SSimpleForm;
import org.swidgets.base.sinputbox.AsyncDataValidator;
import org.swidgets.base.sinputbox.SDecimalInputBox;
import org.swidgets.base.sinputbox.SInputBox;
import org.swidgets.base.sinputbox.SIntegerInputBox;
import org.swidgets.base.sinputbox.SRegexInputBox;
import org.swidgets.base.slabel.SLabel;
import org.swidgets.base.smessagebox.ConfirmCallBack;
import org.swidgets.base.smessagebox.SMessageBox;
import org.swidgets.base.spanels.SFormPanel;
import org.swidgets.base.spanels.SSimplePanel;
import org.swidgets.base.spanels.STitledPanel;
import org.swidgets.sComboBox.SComboBox;
import org.swidgets.sTabPanel.STabPanel;
import org.swidgets.sinputdata.SInputData;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public final class SWidgetsGallery {
	public static Widget getTitle(){
		SSimplePanel  sSimplePanel = new SSimplePanel();							
		
		SLabel title = new SLabel("Widgets Gallery");
		title.setStyle(SLabel.TITLE_LABEL_STYLE);		
		
		sSimplePanel.add(title);		
		
		return sSimplePanel;
	}

	public static Widget getDemoSTabPanel(){
		STabPanel tabPanel = new STabPanel();

		SSimplePanel p1 = new SSimplePanel();
		p1.setPixelSize(300, 150);
		p1.add(new SLabel("Corpo 1"));

		SSimplePanel p2 = new SSimplePanel();
		p2.setPixelSize(300, 150);
		p2.add(new SLabel("Corpo 2"));

		SSimplePanel p3 = new SSimplePanel();
		p3.setPixelSize(300, 150);
		p3.add(new SLabel("Corpo 3"));

		tabPanel.add(p1, "Tab 1");
		tabPanel.add(p2, "Tab 2");
		tabPanel.add(p3, "Tab 3");
		tabPanel.selectTab(0);
		
		return tabPanel; 		
	}
		
	public static Widget getDemoSLabel() {
		STitledPanel  labelTitledPanel = new STitledPanel("Label");
		VerticalPanel vpLabel = new VerticalPanel();
		
		labelTitledPanel.add(vpLabel);
		vpLabel.setSpacing(20);
		
		vpLabel.add(new SLabel("Title",SLabel.TITLE_LABEL_STYLE));
		vpLabel.add(new SLabel("SmallTitle",SLabel.SMALL_TITLE_LABEL_STYLE));
		vpLabel.add(new SLabel("Default",SLabel.DEFAULT_LABEL_STYLE));
		vpLabel.add(new SLabel("Small",SLabel.SMALL_TITLE_LABEL_STYLE));
		
		return labelTitledPanel;		
	}
	
	public static Widget getDemoMessageBox(){
		STitledPanel  messageBoxTitledPanel = new STitledPanel("Message Box");
		HorizontalPanel hpMessageBox = new HorizontalPanel();

		Button sButtonDialog = new Button("Show Dialog");
		sButtonDialog.addClickListener(new ClickListener(){

			public void onClick(Widget sender) {
				SMessageBox.alert("Alert","Title", null );

			}});

		Button sButtonConfirm = new Button("Show Confirm");
		sButtonConfirm.addClickListener(new ClickListener(){

			public void onClick(Widget sender) {
				SMessageBox.confirm("Confirm?","Title", new ConfirmCallBack(){
					public void onCancel() {}
					public void onConfirm() {}
					public void onNo() {Window.alert("No");}
					public void onYes() {Window.alert("Yes");}

				});

			}});

		Button sButtonYesNoCancel = new Button("Show YesNoCancel");
		sButtonYesNoCancel.addClickListener(new ClickListener(){

			public void onClick(Widget sender) {
				SMessageBox.yesNowCancel("Show YesNoCancel","Title", new ConfirmCallBack(){
					public void onCancel() {}
					public void onConfirm() {}
					public void onNo() {}
					public void onYes() {
						Window.alert("aaa");
						
					}

				});

			}});

		messageBoxTitledPanel.add(hpMessageBox);
		hpMessageBox.add(sButtonConfirm);
		hpMessageBox.add(sButtonYesNoCancel);
		hpMessageBox.add(sButtonDialog);
		hpMessageBox.setSpacing(20);
		
		return messageBoxTitledPanel;
	}
	
	public static Widget getDemoInputBox(){
		STitledPanel  inputTitledPanel = new STitledPanel("InputBox");
		VerticalPanel vpInputBox = new VerticalPanel();								
		HorizontalPanel hpInputBox = new HorizontalPanel();
		vpInputBox.add(hpInputBox);
		SInputBox sInputBox = new SInputBox("Simple ");		
		final SIntegerInputBox sIntegerInputBox = new SIntegerInputBox("Integer");
		SDecimalInputBox sDecimalInputBox = new SDecimalInputBox("Decimal");
		SRegexInputBox sRegexInputBox = new SRegexInputBox("RegExp","^(abc){0,1}$", "Valore non valido, accettato solo 'abc'");
		
		inputTitledPanel.add(vpInputBox);
		sIntegerInputBox.setRequied(true);
		sIntegerInputBox.setWidth("20px");
		
		sIntegerInputBox.setAsyncDataValidator(new AsyncDataValidator(){

			public void validate(String data, AsyncCallback callback) {
				
				if (!data.equalsIgnoreCase("123")){
					sIntegerInputBox.errorOn("IL MIO VALIDATORE ACCETTA SOLO 123");
				}
				
			}
			
		});
		sRegexInputBox.setEnabled(false);
		inputTitledPanel.add(vpInputBox);
		vpInputBox.setSpacing(20);
		vpInputBox.add(sIntegerInputBox);
		hpInputBox.add(sDecimalInputBox);
		hpInputBox.add(new SLabel("stringa",SLabel.TITLE_LABEL_STYLE));
		vpInputBox.add(sInputBox);
		vpInputBox.add(sIntegerInputBox);
		vpInputBox.add(sRegexInputBox);
		
		return inputTitledPanel;		
	}
	
	public static Widget getFormPanel (){
		SFormPanel formPanel = new SFormPanel("Form Panel","300px","100px");
		SDecimalInputBox sDecimalInputBox01 = new SDecimalInputBox("numero 1");
		SDecimalInputBox sDecimalInputBox02 = new SDecimalInputBox("numero 2");
		SDecimalInputBox sDecimalInputBox03 = new SDecimalInputBox("numero 3");
		VerticalPanel panel = new VerticalPanel();
		panel.setSpacing(4);
		SLabel formPanelLabel= new SLabel("Frame Panel Body");
		panel.add(formPanelLabel);
		panel.add(sDecimalInputBox01);
		panel.add(sDecimalInputBox02);
		panel.add(sDecimalInputBox03);
		formPanel.add(panel);
		return formPanel;
		
	}
	
	public static Widget getForm(){
		final SSimpleForm simpleFormPanel = new SSimpleForm("Example");
		SDecimalInputBox sDecimalInputBox01 = new SDecimalInputBox("numero 1");
		SDecimalInputBox sDecimalInputBox02 = new SDecimalInputBox("numero 2");
		sDecimalInputBox02.setRequied(true);
		SDecimalInputBox sDecimalInputBox03 = new SDecimalInputBox("numero 3");
		
		sDecimalInputBox01.setRequied(true);
		sDecimalInputBox01.setText("1111 user");
		sDecimalInputBox01.setAsyncDataValidator(new AsyncDataValidator(){

			public void validate(final String data, final AsyncCallback callback) {
				// TODO Auto-generated method stub
				callback.onSuccess("");
			}
			
		});
//		sDecimalInputBox02.setText("22222");
//		sDecimalInputBox02.setAsyncDataValidator(new AsyncDataValidator(){
//			
//			public void validate(String data, final AsyncCallback callback) {
//				// TODO Auto-generated method stub
//				
//				TestServiceFactory.getTestServiceAsync().getData(0, 10, new AsyncCallback(){
//					public void onFailure(Throwable caught) {
//						callback.onFailure(new Exception("3333333"));
//					}
//
//					public void onSuccess(Object result) {
//						if(result!=null)
//							callback.onSuccess(result);
//						else
//							callback.onFailure(new Exception("222222"));
//					}
//					
//				});
//			}
//			
//		});
		simpleFormPanel.setField(1, 1, sDecimalInputBox01);
		simpleFormPanel.setField(1, 2, sDecimalInputBox02);
		simpleFormPanel.setField(1, 3, sDecimalInputBox03);
		Button sButton = new Button("Valida Form");
		simpleFormPanel.addButton(sButton);
		sButton.addClickListener(new ClickListener(){

			public void onClick(Widget sender) {
				simpleFormPanel.validate(new AsyncCallback(){

					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						GWT.log("non SALVO", null);
					}

					public void onSuccess(Object result) {
						GWT.log("SALVO", null);
						
					}});
				
				
			}});
		
		return simpleFormPanel;
		
	}
	
	public static Widget getInputData(){
		VerticalPanel vp=new VerticalPanel();
		Date data=new Date();
		data.setYear(2008);
		data.setMonth(2);
		data.setDate(5);
		//vp.add(new SInputData(data));
		vp.add(new SInputData("Data",data));
		vp.add(new SLabel("--"));
		vp.add(new SLabel("--"));
		vp.add(new SLabel("--"));
		vp.add(new SLabel("--"));
		vp.add(new SLabel("label di prova per testare la trasparenza del calendario"));
		
		return vp;
	}
	
	public static Widget getComboBox(){
		SFormPanel sform=new SFormPanel("ComboBox","100%","40px");
		VerticalPanel vp=new VerticalPanel();
		HorizontalPanel hp=new HorizontalPanel();
		SComboBox comboTest = new SComboBox();
		comboTest.setRequied(true);

		comboTest.addOption("4","Michele Piccirillo");
		comboTest.addOption("3","Meg Griffin");
		comboTest.addOption("1","Bart Simpson");
		comboTest.addOption("2","Peter Griffin");
		comboTest.addOption("5","Matteo Serra");
		comboTest.addOption("6","Homer Simpson");
		comboTest.addOption("7","Lisa Simpson");

		comboTest.loadList();
		//comboTest.setDropDownListHeight("50px");
		sform.add(vp);
		vp.add(hp);
		hp.add(new SLabel("ComboBox"));
		hp.add(comboTest);
		vp.add(new SLabel("label di prova"));
		return sform;		
	}
	
}
